package org.mch.start;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import org.apache.commons.io.FileUtils;
import org.mch.swingm.core.MButton;
import org.mch.swingm.core.MDialog;

public class About extends MDialog {
	private static final long serialVersionUID = -6879769284725878504L;

	private final JPanel contentPanel = new JPanel();
	private String LicenseText;
	private String AboutText;

	private JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
	private JEditorPane editorPane = new JEditorPane();
	private JTextArea textArea = new JTextArea();
	private MButton okButton = new MButton("确定");
	private final JPanel panel_1 = new JPanel();
	private MButton btn_mchweb;
	private MButton btn_bbsmchweb;
	private MButton btn_zmcweb;
	private MButton btn_meimei6;
	private MButton btn_meimei8;

	public static void main(String[] args) {
		try {
			About dialog = new About();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public About() {
		super();
		try {
			initGUI();
		} catch (Exception e) {
			log().error(e);
		}
	}

	public About(Frame owner) {
		super(owner);
		try {
			initGUI();
		} catch (Exception e) {
			log().error(e);
		}
	}

	private void initGUI() throws Exception {
		setFont(new Font("宋体", Font.PLAIN, 12));
		File aboutFile = new File(getClass().getResource("/resource/config/about.txt").getFile());
		File licenseFile = new File(getClass().getResource("/resource/config/license.txt").getFile());
		this.LicenseText = FileUtils.readFileToString(licenseFile, "UTF8");
		this.AboutText = FileUtils.readFileToString(aboutFile, "UTF8");

		setTitle("关于 MCH");
		setBounds(100, 100, 691, 396);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		tabbedPane.setFont(new Font("宋体", Font.PLAIN, 12));
		contentPanel.add(tabbedPane, BorderLayout.CENTER);
		JPanel panel = new JPanel();
		tabbedPane.addTab("关于", null, panel, null);
		panel.setLayout(new BorderLayout(0, 0));
		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane, BorderLayout.CENTER);
		editorPane.setFont(new Font("宋体", Font.PLAIN, 12));
		editorPane.setEditable(false);
		editorPane.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		scrollPane.setViewportView(editorPane);
		editorPane.setText(this.AboutText);

		JPanel panel1 = new JPanel();
		tabbedPane.addTab("许可证", null, panel1, null);
		panel1.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane1 = new JScrollPane();
		panel1.add(scrollPane1, BorderLayout.CENTER);
		textArea.setFont(new Font("宋体", Font.PLAIN, 12));
		textArea.setEditable(false);
		textArea.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		scrollPane1.setViewportView(textArea);
		textArea.setText(this.LicenseText);
		JPanel buttonPane = new JPanel();
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		okButton.setFont(new Font("宋体", Font.PLAIN, 12));
		okButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				About.this.dispose();
			}
		});

		buttonPane.setLayout(new BorderLayout(0, 0));
		okButton.setActionCommand("OK");
		buttonPane.add(okButton, BorderLayout.EAST);
		getRootPane().setDefaultButton(okButton);
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel_1.setFont(new Font("宋体", Font.PLAIN, 12));
		panel_1.setBorder(new TitledBorder(null, "友情连接", TitledBorder.LEADING, TitledBorder.TOP,
				new Font("宋体", Font.PLAIN, 12), null));

		buttonPane.add(panel_1, BorderLayout.CENTER);

		panel_1.add(getBtn_mchweb());
		panel_1.add(getBtn_bbsmchweb());
		panel_1.add(getBtn_zmcweb());
		panel_1.add(getBtn_meimei6());
		panel_1.add(getBtn_meimei8());
	}

	public MButton getBtn_mchweb() {
		if (btn_mchweb == null) {
			btn_mchweb = new MButton("明春网");
			btn_mchweb.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try {
						Runtime.getRuntime().exec("cmd.exe /c start " + "http://www.mchweb.net");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return btn_mchweb;
	}

	public MButton getBtn_bbsmchweb() {
		if (btn_bbsmchweb == null) {
			btn_bbsmchweb = new MButton("明春论坛");
			btn_bbsmchweb.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try {
						Runtime.getRuntime().exec("cmd.exe /c start " + "http://bbs.mchweb.net");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return btn_bbsmchweb;
	}

	public MButton getBtn_zmcweb() {
		if (btn_zmcweb == null) {
			btn_zmcweb = new MButton("明春资源网");
			btn_zmcweb.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try {
						Runtime.getRuntime().exec("cmd.exe /c start " + "http://www.zmcweb.com");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return btn_zmcweb;
	}

	public MButton getBtn_meimei6() {
		if (btn_meimei6 == null) {
			btn_meimei6 = new MButton("MM6");
			btn_meimei6.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try {
						Runtime.getRuntime().exec("cmd.exe /c start " + "http://www.meimei6.net");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return btn_meimei6;
	}

	public MButton getBtn_meimei8() {
		if (btn_meimei8 == null) {
			btn_meimei8 = new MButton("MM8");
			btn_meimei8.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try {
						Runtime.getRuntime().exec("cmd.exe /c start " + "http://www.meimei8.net");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return btn_meimei8;
	}

}
